<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="机构名称" prop="organName">
        <el-input
          v-model="queryParams.organName"
          placeholder="请输入机构名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="机构电话" prop="companyphone">
        <el-input
          v-model="queryParams.companyphone"
          placeholder="请输入机构电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['iot:organ:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['iot:organ:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="organList"
      @selection-change="handleSelectionChange"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      row-key="organId"
    >
      <el-table-column label="机构名称" align="center" prop="organName" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="机构电话" align="center" prop="companyphone" />
      <el-table-column label="机构地址" align="center" prop="companyAddress" />
      <el-table-column label="机构负责人" align="center" prop="institutional" />
      <el-table-column label="机构介绍" align="center" prop="companyProfile" />
      <el-table-column label="机构状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['iot:organ:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['iot:organ:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改机构管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级菜单" prop="organId">
              <treeselect
                v-model="form.organId"
                :options="organLists"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级菜单"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="24">
          <el-form-item label="机构名称" prop="organName">
            <el-input v-model="form.organName" placeholder="请输入机构名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="显示顺序" prop="orderNum">
            <el-input-number
              v-model="form.orderNum"
              label="请输入显示顺序"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="机构电话" prop="companyphone">
            <el-input
              v-model="form.companyphone"
              placeholder="请输入机构电话"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="机构地址" prop="companyAddress">
            <el-input
              v-model="form.companyAddress"
              placeholder="请输入机构地址"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="机构介绍" prop="companyProfile">
            <el-input
              v-model="form.companyProfile"
              placeholder="请输入机构介绍"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="机构负责人" prop="institutional">
            <el-input
              v-model="form.institutional"
              placeholder="请输入机构负责人"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="机构状态" prop="status">
            <el-switch
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入内容" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listOrgan,
  getOrgan,
  delOrgan,
  addOrgan,
  updateOrgan,
} from "@/api/user/list.js";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Organ",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机构管理表格数据
      organList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        organName: null,
        parentId: null,
        orderNum: null,
        companyphone: null,
        customerphone: null,
        companyAddress: null,
        companyProfile: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        organId: [
          { required: true, message: "上级菜单不能为空", trigger: "change" },
        ],
        organName: [
          { required: true, message: "机构名称不能为空", trigger: "blur" },
        ],
        companyphone: [
          { required: true, message: "机构电话不能为空", trigger: "blur" },
        ],
        companyAddress: [
          { required: true, message: "机构地址不能为空", trigger: "blur" },
        ],
        institutional: [
          { required: true, message: "机构负责人不能为空", trigger: "blur" },
        ],
       
      },
      organLists: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.organId,
        label: node.organName,
        children: node.children,
      };
    },
    /** 查询机构管理列表 */
    getList() {
      this.loading = true;
      listOrgan(this.queryParams).then((response) => {
        //   this.organList = response.rows;
        console.log(response.rows, "console.console.console");
        this.organList = this.handleTree(response.rows, "organId");

        const menu = { organId: 0, organName: "主类目", children: [] };
        menu.children = this.handleTree(response.rows, "organId");
        this.organLists.push(menu);
        console.log(this.organLists, "console.console.console.");
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        organId: null,
        organName: null,
        parentId: null,
        orderNum: null,
        companyphone: null,
        customerphone: null,
        companyAddress: null,
        companyProfile: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.organId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机构管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const organId = row.organId || this.ids;
      getOrgan(organId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改机构管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.form, "-----------------------");
          if (this.form.parentId != null) {
            updateOrgan(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.organLists=[]

              this.getList();
            });
          } else {
            this.form.parentId=this.form.organId
            addOrgan(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.organLists=[]
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const organIds = row.organId || this.ids;
      this.$modal
        .confirm('是否确认删除机构名称为"' + row.organName + '"的数据项？')
        .then(function () {
          return delOrgan(organIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "iot/organ/export",
        {
          ...this.queryParams,
        },
        `organ_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>